Masthead

Writing Readable R Code

The R code you may find on the web can be a little hard to read. Below is a sample that displays a grid of covariates.

# Pairs plots with smoothScatter, Pearson’s and histograms

v1=0:100
v2=200:300
v3=100:200
v4=100:200

z=cbind(v1, v2, v3, v4)  ## variables to assess

panel.pearson <- function(x, y, ...) {  ## upper panel display for Pearson’s
  horizontal <- (par("usr")[1] + par("usr")[2]) / 2;
  vertical <- (par("usr")[3] + par("usr")[4]) / 2;
  text(horizontal, vertical, format(abs(cor(x,y)),  digits=2, cex=2))
}
pairs(z, cex.axis=1.3,  cex.labels=3, lower.panel=function(...){par(new=TRUE);smoothScatter(..., nrpoints=0)},
      diag.panel=function(...){par(new=TRUE);hist(..., main="", breaks=50, col="orange", nrpoints=0)}, 
	upper.panel=panel.pearson) ### create histogram in the diagonal panel and density scatter in the lower panel

The line with "panel.pearson" creates a function that organizes the data into a grid using "horizontal" and "vertical".

The really challenging part to read is the line with the "pairs()" function. Notice that the writer has created a number of functions that are created in the parameter list and then passed into "pairs". I do not recommend this approach. The other problem is that we have to put this code in anywhere we want to use this chart.

In the code below, I've created a function that takes a series of variables which can then be called in a much simpler way than the code above. The complexity is hidden in the function. This code can then be saved in a file and used in any script we need it in.

##############################################################################
# Utility functions to diplay
# Pairs plots with smoothScatter, Pearson's and histograms
##############################################################################

##############################################################################
# Function to display the pearsons values in the upper-right panels
# For additional information on the graphic parameters:
#  https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/par.html
##############################################################################
PearsonPanel <- function(x, y, ...) 
{          
  horizontal <- (par("usr")[1] + par("usr")[2]) / 2;
  vertical <- (par("usr")[3] + par("usr")[4]) / 2;
  
  CorrelationMatrix=cor(x,y) # Create the correlation matrix between x and y
  
  # Format the text (cex=scale factor for font size, digits=sig digits)
  FormattedMatrix=format(abs(CorrelationMatrix),  digits=2, cex=5)
  
  # Function to put the correlation values into the panels
  text(horizontal, vertical, FormattedMatrix)
}

##############################################################################
# Function to display the smoothed scatttergrams in the lower-left panels
##############################################################################
LowerPanel=function(...)
{
  par(new=TRUE); # sets the graphic parameter "new" to true (keeps the drawing frame from being erased)
  smoothScatter(...) # parameters passed are sent as "..."
} 

##############################################################################
# This is the dialogal plots with histograms
##############################################################################
DiagonalPanel=function(...)
{
  par(new=TRUE);
  hist(..., main="", breaks=50, col="orange")
}

##############################################################################
# This is the main function to call to display the scatter plots
##############################################################################
NiceScatterPlot=function(z)
{
  # Create a matrix of scatter plots
  DiagonalLabelFontSize=3
  AxisFontSize=1.3
  pairs(z,  #data frame with value
        cex.axis=AxisFontSize,  cex.labels=DiagonalLabelFontSize, 
        lower.panel=LowerPanel,
        diag.panel=DiagonalPanel, 
        upper.panel=PearsonPanel) ### create histogram in the diagonal panel and density scatter in the lower panel
  
}

Now, we can include the code above from a script that is saved separately and call the function very easily:

#############################################################################
# Main script to show two approaches to writing R code.
#############################################################################

# Including the utilities folder with "NiceScatterPlot()"
source("D:\\ProjectsR\\Utilities\\ScatterPlots.R")

# vectors to test.  Try different values here.
v1=0:100
v2=200:300
v3=100:200
v4=100:200

# Vectors converted into a table
z=cbind(v1, v2, v3, v4)  # variables to assess

#############################################################################
# This is the original code that while it works, is a little difficult for
# me to understand and modify.
#############################################################################

panel.pearson <- function(x, y, ...) { ## upper panel display for Pearson’s
  horizontal <- (par("usr")[1] + par("usr")[2]) / 2;
  vertical <- (par("usr")[3] + par("usr")[4]) / 2;
  text(horizontal, vertical, format(abs(cor(x,y)),  digits=2, cex=2))
}

pairs(z, cex.axis=1.3,  cex.labels=3, lower.panel=function(...){par(new=TRUE);smoothScatter(..., nrpoints=0)},
      diag.panel=function(...){par(new=TRUE);hist(..., main="", breaks=50, col="orange", nrpoints=0)}, upper.panel=panel.pearson) ### create histogram in the diagonal panel and density scatter in the lower panel

#############################################################################
# This is a new function that is better structured and will be easier to
# document and understand and thus easier to support.  See "ScatterPlots.R"
# for more information.
#############################################################################
NiceScatterPlot(z) 

Please strive to make your R code readable so others can use it and to encourage everyone to write readable code!

Additional Resources

Quick-R Scatter Plots

© Copyright 2018 HSU - All rights reserved.